//
//  AppDelegate.m
//  nativeNtification
//
//  Created by wanglei on 17/2/19.
//  Copyright © 2017年 wanglei. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end



@implementation AppDelegate


-(void)jump:(int)key
{
    //不同的状态进来，打开不同的网页
    if (key == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.hudongdong.com"]];
    }
    else if (key == 2){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
    }
    
}



/*
 软件已经主动退出，或者后台超过时间退出，点击通知栏消息之后就只会调用这个函数
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    /*
     说明：当应用程序启动时执行，应用程序启动入口。只在应用程序启动时执行一次。application参数用来获取应用程序的状态、变量等，值得注意的是字典参数：(NSDictionary *)launchOptions，该参数存储程序启动的原因。
     若用户直接启动，lauchOptions内无数据;
     若由其他应用程序通过openURL:启动，则UIApplicationLaunchOptionsURLKey对应的对象为启动URL（NSURL）,UIApplicationLaunchOptionsSourceApplicationKey对应启动的源应用程序的bundle ID (NSString)；
     若由本地通知启动，则UIApplicationLaunchOptionsLocalNotificationKey对应的是为启动应用程序的的本地通知对象(UILocalNotification)；
     若由远程通知启动，则UIApplicationLaunchOptionsRemoteNotificationKey对应的是启动应用程序的的远程通知信息userInfo（NSDictionary）；
     其他key还有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,
     UIApplicationLaunchOptionsNewsstandDownloadsKey。 如果要在启动时，做出一些区分，那就需要在下面的代码做处理。 比如：应用可以被某个其它应用调起（作为该应用的子应用），要实现单点登录，那就需要在启动代码的地方做出合理的验证，并跳过登录。
     */

    
    
    // 这里主要是考虑到远程通知的情况,本demo里面暂时没有用处
    UILocalNotification * local = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    
    if ([local.userInfo[@"name"] isEqualToString:@"Damon"]) {
        NSLog(@"damon");
        [self jump:2];
    }
    return YES;
}
/*
 在本地消息调用之后，就会在通知栏显示了，点击通知栏的消息之后，会自动打开这个软件，需要注意的是软件不同的状态，点击通知栏调用的函数也不同。
 */
// 软件如果是在后台运行，并没有退出，调用的是AppDelegate的这个函数

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"Application did receive local notifications");
    // 在这里写跳转代码
    // 如果是应用程序在前台,依然会收到通知,但是收到通知之后不应该跳转
    if (application.applicationState == UIApplicationStateActive)
    {
        return;
    }
    
    // 当应用在后台收到本地通知时执行的跳转代码
    if (application.applicationState == UIApplicationStateInactive) {
        //可以通过设置通知时的userinfo过滤某条通知
        if ([notification.userInfo[@"name"] isEqualToString:@"Damon"]) {
            NSLog(@"damon");
        }
        //可以得到所有的通知
        for (UILocalNotification *noti in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
            NSLog(@"接收到的通知共有%@",noti.fireDate);
        }
        //通知之后可以取消对应的通知
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
    [self jump:1];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
